package com.xuequ.answerme.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.adapter.FocusRecyclerViewAdapter;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.details.QuestionDetailActivity;
import com.xuequ.answerme.manager.AnswerManager;
import com.xuequ.answerme.manager.QuestionManager;
import com.xuequ.answerme.manager.UserManager;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by shijie on 2015/10/20.
 */
public class SearchActivity extends AppCompatActivity {

    private Toolbar toolbar;

    private View contentView;
    private MaterialDialog materialDialog;
    private TextView answererText;
    private TextView answerContentText;
    private TextView LikeText;
    private TextView OKText;
    private TextView questionPreviewTitle;
    private TextView questionPreviewDesc;
    private TextView detailText;

    private ImageView mSearchImg;
    private EditText mSearchEdit;

    private RecyclerView mRecyclerView;
    private List<QuestionBean> mData;
    private FocusRecyclerViewAdapter mAdapter;

    private UserManager userManager;
    private AnswerManager answerManager;
    private QuestionManager questionManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initViews();
    }

    private void initViews() {

        if (userManager == null) {
            userManager = new UserManager();
        }
        if (answerManager == null) {
            answerManager = new AnswerManager();
        }
        if (questionManager == null) {
            questionManager = new QuestionManager();
        }

        toolbar = (Toolbar) findViewById(R.id.search_tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mSearchEdit = (EditText) findViewById(R.id.search_edit);

        mSearchImg = (ImageView) findViewById(R.id.search_img);
        mSearchImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("SearchActivity","click search");
                //search the Question
                if (!TextUtils.isEmpty(mSearchEdit.getText())) {
                    Log.i("SearchActivity","target is not empty");
                    //search
                    mSearchImg.setClickable(false);
                    questionManager.fuzzySearchQuestion(SearchActivity.this, mSearchEdit.getText().toString(), new QuestionManager.FuzzySearchQuestionListener() {
                        @Override
                        public void fuzzySearchQuestionOnSuccess(List<QuestionBean> questions) {
                            if (questions.size() != 0) {
                                Log.i("SearchActivity","search success");
                                mAdapter.refreshData(questions);
                            } else {
                                Log.i("searchActivity","search success no target question");
                                mSearchImg.setClickable(true);
                            }
                        }

                        @Override
                        public void fuzzySearchQuestionOnFailure(String failureMsg) {
                            Log.i("SearchActivity","search failure "+failureMsg);
                            mSearchImg.setClickable(true);
                        }
                    });
                }
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.search_recycler);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(SearchActivity.this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);

        mData = new ArrayList<>();
        mAdapter = new FocusRecyclerViewAdapter(mData);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new FocusRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                // turn to QuestionDetailActivity
                Intent intent = new Intent(SearchActivity.this, QuestionDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("questionBean", mData.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                //
                previewQuestion(position);
            }
        });
        mAdapter.setOnRefreshDoneListener(new FocusRecyclerViewAdapter.OnRefreshDoneListener() {
            @Override
            public void onRefreshDone() {
                mSearchImg.setClickable(true);
            }
        });

    }

    private void previewQuestion(int position) {

        contentView = View.inflate(SearchActivity.this, R.layout.question_preview_layout, null);

        final QuestionBean questionPreview = mData.get(position);

        questionPreviewTitle = (TextView) contentView.findViewById(R.id.question_preview_title_text);
        questionPreviewTitle.setText(questionPreview.getQuestion_title());

        questionPreviewDesc = (TextView) contentView.findViewById(R.id.question_preview_description_text);
        questionPreviewDesc.setText(questionPreview.getQuestion_description());

        detailText = (TextView) contentView.findViewById(R.id.question_preview_details_text);
        detailText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, QuestionDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("questionBean", questionPreview);
                intent.putExtras(bundle);
                startActivity(intent);
                materialDialog.dismiss();
            }
        });

        OKText = (TextView) contentView.findViewById(R.id.question_preview_ok_text);
        OKText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDialog.dismiss();
            }
        });

        materialDialog = new MaterialDialog(SearchActivity.this).setView(contentView);
        materialDialog.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
